<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
?>
<SCRIPT type="text/javascript" src="/view/javascripts/form.js"></SCRIPT>
<SCRIPT type="text/javascript" src="/view/javascripts/indicator.js"></SCRIPT>

<?php 
if($response->isParameter('process')) {
	$process = $response->getParameter('process');
	// список показателей данного процесса
	// они потенциальные родительские показатели
	// только при отображении нужно будет исключить текущий показатель
	$indicators = $process->getIndicators(); 
		
	$today = getdate();
?>	
	
<?php
	if($response->isParameter('indicator')) {
		
		$indicator = $response->getParameter('indicator');
			
		$id = $indicator->getIdentity();
				
		$name = $indicator->getName();
		$weight = $indicator->getWeight();
		$objective = $indicator->isObjective();
		$year = $indicator->getYear();
		$plan = $indicator->getPlanValue();
		$idUnit = $indicator->getUnit()->getIdentity();
		
		$parent = $indicator->getParent();
		$childrens = $indicator->getChildren();
		
	} else {
		$id = null;
		$name = '';
		$weight = 0;
		$objective = true;
		
		$year = $today[year];
		$plan = null;
 	}
?>
<DIV class="block">
	<H1><A href="/editing/process/index.html">Процессы</A> &raquo;
	<A href="/editing/process/edit.html">Процесс ::
	<?php echo $process->getCode();?>
	</A> &raquo; Редактирование показателя ::
	<?php
		if($id !== null) {
			echo $name;
		} else {
			echo "Новый показатель";
		}
	?>
	</H1>

	<FORM action="/editing/indicator/update.html" method="post" id="editindicator">
	<?php
		if($response->isParameter('indicator')) {
	?>
		<INPUT type="hidden" name="id_indicator" value="<?php echo $id;?>">
	<?php
		}
	?>
		<TABLE>
		<TR>
			<TD><LABEL for = "name">Название показателя:</LABEL></TD>
			<TD><INPUT type="text" id="name" name="name"
				value="<?php echo $name;?>"
				pattern="<?php echo Pattern::SENTENCE;?>" 
				message="<?php echo Error::INCORRECT_IDICATOR_NAME;?>" 
				maxlength="150" size = "75">
				</TD>
			</TR>
		<TR>
			<TD><LABEL for = "id_parent_indicator">Родительский показатель:</LABEL></TD>
			<TD><SELECT name="id_parent_indicator" id="id_parent_indicator">
				<OPTION></OPTION>
			<?php
				foreach($indicators as $parentElement) {
					showParent($parentElement, 0, $parent, $id);
				}
			?>
				</SELECT>
			</TD>
		</TR>
		<TR>
			<TD><LABEL for = "objective">Целевой:</LABEL> </TD>
			<TD><INPUT type = "checkbox" id = "objective" name = "objective" 
			<?php 
				if($objective){
					echo "checked='checked'";
				}					
			?>
			></TD>
				</TR>
				
				<TR>
					<TD><LABEL for = "id_unit">Единица измерения</LABEL></TD>
					<TD>
					
					<?php 
			   			if($response->isParameter('units')) {
					?>
					<SELECT id="id_unit" name="id_unit">
					<?php
    					foreach($response->getParameter('units') as $unit) {
					?>
						<OPTION  value="<?php echo $unit->getIdentity();?>" 
							<?php if ($idUnit == $unit->getIdentity()){
								echo "selected";						
							}?>
						>
						<?php echo $unit->getName();?>
					</OPTION>
					<?php
						}
					?>
					</SELECT>
					<?php
						}
					?>
					</TD>
				</TR>
				
				<TR>
					<TD><LABEL for = "plan_value">Плановое значение:</LABEL>
					</TD>
					<TD>
						<INPUT type="text" size ="10" id="plan_value" name="plan_value"
						value="<?php if ($plan){echo $plan;}?>"
						<?php
							if (!$plan){echo ' disabled=false';}
						?>
						pattern="<?php echo Pattern::NUMBER;?>" 
						message="<?php echo Error::INCORRECT_PLAN;?>" 	
						/><INPUT type = "checkbox" id = "plan_define_department" name="plan_define_department" onclick="hidePlan()"
						<?php
							if (!$plan){
								echo "checked='checked'";
							}
						?>
						/><I>Определяется структурным подразделением</I>
					</TD>
				</TR>
				<TR>
					<TD><LABEL for = "weight">Весовой коэффициент:</LABEL></TD>
					<TD><INPUT type="text" size ="10" id="weight" name="weight" value="<?php echo $weight;?>"
					pattern="<?php echo Pattern::NUMBER;?>" 
						message="<?php echo Error::INCORRECT_WEIGHT;?>" 
					/></TD>
				</TR>
				<TR>
					<TD><LABEL for = "year">Год:</LABEL></TD>
					<TD>
						<select id="year" name="year" >
                         <?php
						 //Если есть зависимые данные
                        	if($response->isParameter('countIndicator')) {
								//Выводим только один год(без возможности его изменения)
								echo "<option value=".$year." selected >".$year."</option>";	
								echo "</select>";
								//Вывод сообщения о невозможности редактирования данного поля
								echo "(Для редактирования удалите зависимые данные)";
							}else{
								for($i=$today[year]-1; $i<=$today[year]+1;$i++){		
									echo "<option value=".$i."'";
										if ($i==$year){
											echo " selected ";
										}
									echo ">".$i."</option>";							
								}
								echo "</select>";
							}
						?>
						
                       
					</TD>
				</TR>
		</TABLE>
	</FORM>
</DIV>

<DIV class="block">
	<BUTTON type="button" onclick="submitForm('editindicator')">
		<IMG src="/view/img/button-save.png" alt="">
		<SPAN>сохранить</SPAN>
	</BUTTON>
	<BUTTON type="button" onclick="resetForm('editindicator');openElementAccess('editindicator')">
		<IMG src="/view/img/button-reset.png" alt="">
		<SPAN>сбросить</SPAN>
	</BUTTON>
<?php
	if($response->isParameter('indicator')) {
?>
	<FORM action="/editing/indicator/delete.html" method="post" class="inline">
		<INPUT type="hidden" name="id_indicator" value="<?php echo $id;?>">
		<BUTTON type="submit">
			<IMG src="/view/img/button-delete.png" alt="">
			<SPAN>удалить</SPAN>
		</BUTTON>
	</FORM>
<?php
	}
?>
	<FORM action="/editing/process/edit.html" method="get" class="inline">
		<BUTTON type="submit">
			<IMG src="/view/img/button-back.png" alt="">
			<SPAN>назад</SPAN>
		</BUTTON>
	</FORM>
</DIV>
<?php
	}
?>

<DIV class="block">
	<H1>Исполнители</H1>

<?php 
	if($response->isParameter('departmentTypes')) {
		$departmentTypes = $response->getParameter('departmentTypes');
		
?>

<DIV class="left">
	<FORM id="departmentTypes">
		<TABLE>
		<TR>
			<TH><I>Все подразделения</I></TH>
			<?php
				foreach($departmentTypes as $depType) {
			?>
				<TH><?php echo $depType->getName()."(ы)";?></TH>
			<?php 		
				}
			?>
		</TR>	
		<TR align="center">
			<TD><INPUT type="checkbox" name="selectAll" id="selectAll" onclick="selectAllDepartment()"/></TD>
			<?php
				foreach($departmentTypes as $depType) {
			?>
				<TD>
					<INPUT type="checkbox" name="depType-<?php echo $depType->getIdentity();?>" id="depType-<?php echo $depType->getIdentity();?>" onclick="selectOneDepType()"/>
				</TD>
			<?php	
				}
			?>
		</TR>
		</TABLE>
	</FORM>
</DIV>
<DIV class="block">	
	<BUTTON type="button" id="buttonApply" name="buttonApply"  onclick="select()">
					<IMG src="/view/img/button-reset.png" alt="">
					<SPAN>применить</SPAN>
	</BUTTON>
</DIV>

<?php 
	}
?>
</DIV>
<br/>
<FORM id="responsibilityForDepartments" action="/editing/indicator/responsibility.html" method="post">
<INPUT type="hidden" name="id_indicator" value="<?php echo $id;?>">
<DIV class="block">
	
	<H1>Кварталы отчетности</H1>
	
		<TABLE border="1">
			<TR>
				<TH>I</TH>
				<TH>II</TH>
				<TH>III</TH>
				<TH>IV</TH>
			</TR>
			<TR>
				<TD><INPUT type="checkbox" name="quarter1" id="quarter1" checked="checked"></TD>
				<TD><INPUT type="checkbox" name="quarter2" id="quarter2" checked="checked"></TD>
				<TD><INPUT type="checkbox" name="quarter3" id="quarter3" checked="checked"></TD>
				<TD><INPUT type="checkbox" name="quarter4" id="quarter4" checked="checked"></TD>
			</TR>
		</TABLE>
</DIV>		

<DIV class="block">	
	<H1>Список подразделений</H1>
	
		<TABLE>
		<TR>
			<TH>Тип подразделения</TH>
			<TH>Подразделение-исполнитель</TH>
			<TH>Заполнение</TH>
			<TH>Просмотр</TH>
			<TD class="bgcol">Отменить</TD>
		</TR>
		<?php
			$size = count($response->getParameter('departments')) - 1;
			foreach($response->getParameter('departments') as $i => $department) {
				showDepartment($department, $i == $size);
			}
		?>
		</TABLE>
		
</DIV>
</FORM>
<DIV class="block">
	<BUTTON type="button" onclick="submitForm('responsibilityForDepartments')">
		<IMG src="/view/img/button-save.png" alt="">
		<SPAN>сохранить</SPAN>
	</BUTTON>	
	<BUTTON type="button" onclick="resetForm('editindicator')">
		<IMG src="/view/img/button-reset.png" alt="">
		<SPAN>сбросить</SPAN>
	</BUTTON>
</DIV>

<?php
//---------------------------------------------------------------------------
function showDepartment(Department $department, $isLast = false, $levels = null) {
	$idDepartment = $department->getIdentity();
	$name = $department->getName();
	$type = $department->getType()->getName();
	$idType = $department->getType()->getIdentity();
?>
<TR>
	<TD>
<?php
	if($levels !== null) {
		foreach($levels as $level) {
			if($level) {
?>
		<DIV class="image-1"></DIV>
<?php
			} else {
?>
		<DIV class="image-0"></DIV>
<?php
			}
		}
		$levels[] = !$isLast;
	} else {
		$levels = array();
	}
	if(count($levels) > 0) {
		if($isLast) {
?>
		<DIV class="image-3"></DIV>
<?php
		} else {
?>
		<DIV class="image-2"></DIV>
<?php
		}
	}
	echo $type;
?>
	</TD>
	<TD><?php echo $name;?></TD>
	<TD><INPUT type ="radio" name = "<?php echo 'r-depType-'.$idType.'-'.$idDepartment?>" value="0"/></TD>
	<TD><INPUT type ="radio" name = "<?php echo 'r-depType-'.$idType.'-'.$idDepartment?>" value="1"/></TD>
	<TD class="bgcol"><INPUT type ="radio" name = "<?php echo 'r-depType-'.$idType.'-'.$idDepartment?>" value="2"/></TD>
</TR>
<?php
	$n = count($department->getChildren()) - 1;
	foreach($department->getChildren() as $i => $child) {
		showDepartment($child, $i == $n, $levels);
	}
}
?>

<?php
//---------------------------------------------------------------------------
function showParent(Indicator $parent, $level, $selected, $we) {
	if ($we !== $parent->getIdentity()){
	
?>
<OPTION value="<?php echo $parent->getIdentity();?>"
<?php
	if($selected !== null && $parent->getIdentity() === $selected->getIdentity()) {
?>
	selected="selected"
<?php
	}
?>
>
<?php
	for($i = 0; $i < $level; $i++) {
		echo '&nbsp;&nbsp;&nbsp;';
	}
	echo $parent->getName();
?>
</OPTION>
<?php
	}
	$children = $parent->getChildren();
	foreach($children as $child) {
		showParent($child, $level + 1, $selected,$we);
	}
}
?>